/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

export class ListNode {
    public val: any;
    public next: ListNode|null = null;

    constructor(val: any) {
        this.val = val;
    }
}
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
export const reverseList = function(head: ListNode) {
    let newHead: ListNode|null = null;
    let oldHead: ListNode|null = head;
    let next: ListNode|null = null;

    if (!head) return head;

    while (oldHead) {
        next = oldHead.next;
        oldHead.next = newHead;
        newHead = oldHead;
        oldHead = next;
    }

    return newHead;
};

let head = new ListNode(1);
let node2 = new ListNode(2);
let node3 = new ListNode(3);
head.next = node2;
node2.next = node3;

const newHead = reverseList(head);
console.log(newHead?.val);
console.log(newHead?.next!.val);
console.log(newHead?.next!.val);